package com.jiangcheng.demo.interceptor;


import com.jiangcheng.demo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class AuthenticationInterceptor implements HandlerInterceptor {
//    UserMapper userMapper;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception {
//        String token = httpServletRequest.getHeader("token");// 从 http 请求头中取出 token
//        System.out.println("请求头中的token：" + token);
//        if (token == null){
//            System.out.println("请求失败");
//            return false;
//        }
//        // 验证 token
//        String id = JWT.decode(token).getAudience().get(0);
//        System.out.println("用户ID:" + id);
//        User user = new User();
//        try {
//            user = userMapper.selectById(id);
//            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(user.getPassword())).build();
//            System.out.println("jwtVerifier:" + jwtVerifier);
//        }catch (Exception e){
//            System.out.println("查询不到用户");
//        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest,
                           HttpServletResponse httpServletResponse,
                           Object o, ModelAndView modelAndView) throws Exception {

    }
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest,
                                HttpServletResponse httpServletResponse,
                                Object o, Exception e) throws Exception {
    }
}
